<?php if(!defined('BASEPATH')) exit ('No direct script access allowed');
class C_managebook extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('m_managebook');
	}
	function index(){
		$this->load->view('admin/pages/v_managebook');
	}
	function action(){
		$action = isset($_POST['action']) ? @$_POST['action'] : @$_GET['action'];
		switch($action){
			case "managebook_add":
				$this->managebook_add();
			break;
			case "managebook_edit":
				$this->managebook_edit();
			break;
			case "managebook_delete":
				$this->managebook_delete();
			break;
			case "data_kategori":
				$this->data_kategori();
			break;
			default:
				echo "{ failure : true }";
			break;
		}
	}
	function data_managebook(){
		$key = isset($_GET['key']) ? @$_GET['key'] : @$_POST['key'];
		$result = $this->m_managebook->data_managebook($key);
		echo "{\"results\":" .$result. "}";
	}
	function data_kategori(){
		$result = $this->m_managebook->data_kategori();
		echo "{\"results\":" .$result. "}";
	}
	function managebook_add(){
		$managebook_kode = trim(@$_POST['managebook_kode']);
		$managebook_nama = trim(@$_POST['managebook_nama']);
		$managebook_kategori = trim(@$_POST['managebook_kategori']);
		$managebook_deskripsi = trim(@$_POST['managebook_deskripsi']);
		$managebook_harga = trim(@$_POST['managebook_harga']);
		$managebook_sampul = trim(@$_POST['managebook_sampul']);
		$managebook_file = trim(@$_POST['managebook_file']);
		$managebook_aktif = trim(@$_POST['managebook_aktif']);
		$result = $this->m_managebook->managebook_add($managebook_kode,$managebook_nama,$managebook_kategori,
			$managebook_deskripsi,$managebook_harga,$managebook_sampul,$managebook_file,$managebook_aktif);
		if($result == 1){
			$handle = fopen(base_url(), "r");
			$sampul = file_get_contents('uploads/tempimages/' . $managebook_sampul);
			file_put_contents('uploads/images/' . $managebook_sampul, $sampul);
			$file = file_get_contents('uploads/tempfiles/' . $managebook_file);
			file_put_contents('uploads/files/' . $managebook_file, $file);
			if(file_exists('uploads/images/' . $managebook_sampul)){
				unlink('uploads/tempimages/' . $managebook_sampul);
			}
			if(file_exists('uploads/files/' . $managebook_file)){
				unlink('uploads/tempfiles/' . $managebook_file);
			}
		}
		echo $result;
	}
	function managebook_edit(){
		$managebook_id = trim(@$_POST['managebook_id']);
		$managebook_kode = trim(@$_POST['managebook_kode']);
		$managebook_nama = trim(@$_POST['managebook_nama']);
		$managebook_kategori = trim(@$_POST['managebook_kategori']);
		$managebook_deskripsi = trim(@$_POST['managebook_deskripsi']);
		$managebook_harga = trim(@$_POST['managebook_harga']);
		$managebook_sampul = trim(@$_POST['managebook_sampul']);
		$managebook_file = trim(@$_POST['managebook_file']);
		$managebook_aktif = trim(@$_POST['managebook_aktif']);
		$result = $this->m_managebook->managebook_edit($managebook_id,$managebook_kode,$managebook_nama,$managebook_kategori,
			$managebook_deskripsi,$managebook_harga,$managebook_sampul,$managebook_file,$managebook_aktif);
		if($result == 1){
			$handle = fopen(base_url(), "r");
			$sampul = file_get_contents('uploads/tempimages/' . $managebook_sampul);
			file_put_contents('uploads/images/' . $managebook_sampul, $sampul);
			$file = file_get_contents('uploads/tempfiles/' . $managebook_file);
			file_put_contents('uploads/files/' . $managebook_file, $file);
			if(file_exists('uploads/images/' . $managebook_sampul)){
				unlink('uploads/tempimages/' . $managebook_sampul);
			}
			if(file_exists('uploads/files/' . $managebook_file)){
				unlink('uploads/tempfiles/' . $managebook_file);
			}
		}
		echo $result;
	}
	function managebook_delete(){
		$managebook_id = trim(@$_POST['managebook_id']);
		$result = $this->m_managebook->managebook_delete($managebook_id);
		echo $result;
	}
}
?>